package game.chiecnonkydieu2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DbAdapter {
	public final static String DATA_NAME="MyDB";
	public final static int DATA_VER=1;
	private static class DBHelper extends SQLiteOpenHelper{
		public DBHelper(Context c) {
			super(c,DATA_NAME,null,DATA_VER);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql="CREATE TABLE "+"HIGHSCORE("+"ID INTEGER PRIMARY KEY AUTOINCREMENT,"+"NAME TEXT,"+"SCORE INTEGER NOT NULL)";
			try{
			db.execSQL(sql);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " +"HighScore");
			onCreate(db);
			
		}
		
	}
	final Context context;
	DBHelper dbhelper;
	SQLiteDatabase data;
	public DbAdapter(Context c) {
		context=c;
		dbhelper=new DBHelper(context);
	}
	public DbAdapter open() throws SQLiteException{
		data=dbhelper.getWritableDatabase();
		return this;
	}
	public void close(){
		dbhelper.close();
	}
	public long insertHighScore(String name, int Score){
		ContentValues cv=new ContentValues();
		cv.put("NAME", name);
		cv.put("SCORE", Score);
		return data.insert("HIGHSCORE",null, cv);
	}
	public Cursor getScore(){
		return data.query("HIGHSCORE",null,null,null,null,null,null);
	}
	public boolean updateScore(int id,String name, int score){
		ContentValues cv=new ContentValues();
		cv.put("NAME",name);
		cv.put("SCORE", score);
//		data.insert("HIGHSCORE", null, cv);
		return data.update("HIGHSCORE",cv,"ID="+id,null)>0;
	}
	public boolean deleteScore(int id){
		return data.delete("HIGHSCORE","ID="+id,null)>0;
	}
}
